﻿using System;
using System.Collections.Generic;

namespace Infrastructure.CrossCutting.Extensions
{
    public static class CollectionExtensions
    {
        public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
        {
            foreach (var item in items)
            {
                action(item);
            }
        }

        public static void LoadEnumsFromStringList<T>(this ICollection<T> items, string itemsStringList) where T : struct
        {
            var itemList = itemsStringList.Split(",".ToCharArray());
            foreach (var item in itemList)
            {
                T grayType;
                if (!Enum.TryParse(item, out grayType))
                {
                    throw new Exception("Unknown type defined in configuration: " + itemsStringList);
                }

                items.Add(grayType);
            }
        }
    }
}
